import { loginByEmail, logout, getInfo, exueLogin } from 'api/login';
import { checkThirdLogin } from 'api/platform';
import Cookies from 'js-cookie';
import sha256 from 'sha256';

const user = {
    state: {
        user: '',
        status: '',
        // email: '',
        code: '',
        uid: Cookies.get('uid'),
        roleId: Cookies.get('roleId'),
        oldRoleId: Cookies.get('oldRoleId'),  // add by cuihd 20180620
        authType: '',
        token: Cookies.get('accessToken'),
        username: Cookies.get('username'),
        exueUsername: Cookies.get('exueUsername'),
        name: '',
        avatar: '',
        introduction: '',
        roles: [],
        menuGenerated: false,
        setting: {
            articlePlatform: []
        },
        userType: Cookies.get('userType'),
        roleType: Cookies.get('roleType'),
        schoolId: Cookies.get('schoolId'),
        schoolName: Cookies.get('schoolName'),
        schoolType: Cookies.get('schoolType'),
        schoolList: [],
        // clientType: Cookies.get('clientType'), //为了统计在线用户的信息 by cuihd 20171229
        teacherName: Cookies.get('teacherName'), //为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
        cardIsOpen: Cookies.get('cardIsOpen'),
        sIdentityId: Cookies.get('sIdentityId'),
        authDuobang: Cookies.get('authDuobang')
    },

    mutations: {
        SET_USER_TYPE: (state, type) => {
            state.userType = type;
        },
        SET_AUTH_TYPE: (state, type) => {
            state.authType = type;
        },
        SET_CODE: (state, code) => {
            state.code = code;
        },
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_UID: (state, uid) => {
            state.uid = uid;
        },
        SET_ROLEID: (state, roleId) => {
            state.roleId = roleId;
        },
        SET_OLDROLEID: (state, oldRoleId) => {  // add by cuihd 20180620
            state.oldRoleId = oldRoleId;
        },
        SET_MENU_GENERATED: (state, menuGenerated) => {
            state.menuGenerated = menuGenerated;
        },
        SET_INTRODUCTION: (state, introduction) => {
            state.introduction = introduction;
        },
        SET_SETTING: (state, setting) => {
            state.setting = setting;
        },
        SET_STATUS: (state, status) => {
            state.status = status;
        },
        SET_NAME: (state, name) => {
            state.name = name;
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar;
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles;
        },
        LOGIN_SUCCESS: () => {
            console.log('login success')
        },
        LOGOUT_USER: state => {
            state.user = '';
        },
        SET_SCHOOLID: (state, schoolId) => {
            state.schoolId = schoolId;
        },
        SET_SCHOOLNAME: (state, schoolName) => {
            state.schoolName = schoolName;
        },
        SET_SCHOOLTYPE: (state, schoolType) => {
            state.schoolType = schoolType;
        },
        SET_SCHOOL_LIST: (state, schoolList) => {
            state.schoolList = schoolList;
        },
        SET_USERNAME: (state, username) => {
            state.username = username;
        },
        SET_EXUEUSERNAME: (state, exueUsername) => {
            state.username = exueUsername;
        },
        SET_ROLETYPE: (state, roleType) => {
            state.roleType = roleType;
        },
        // SET_CLIENTTYPE: (state, clientType) => { //为了统计在线用户的信息 by cuihd 20171229
        //     state.clientType = clientType;
        // },
        SET_ROUTERS: (state, routers) => {
            state.routers = routers;
        },
        SET_TEACHERNAME: (state, teacherName) => { //为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
            state.teacherName = teacherName;
        },
        SET_CARDISOPEN: (state, cardIsOpen) => {
            state.cardIsOpen = cardIsOpen;
        },
        SET_THIRD_PLATFORM: (state, JSON) => {
            state.sIdentityId = JSON.sIdentityId;
            state.authDuobang = JSON.authDuobang;
        }
    },

    actions: {
        // 后台登录
        LoginByUsername({ commit }, userInfo) {
            const username = userInfo.username.trim();
            const password = userInfo.password.trim();
            const isRember = userInfo.rememberUname;
            const pwd = sha256(password);
            return new Promise((resolve, reject) => {
                loginByEmail(username, pwd).then(response => {
                    const data = response.data.content;
                    Cookies.set('uid', data.userId);
                    Cookies.set('accessToken', data.token);
                    Cookies.set('userType', data.userType);
                    Cookies.set('schoolId', data.schoolId);
                    Cookies.set('schoolName', data.schoolName);
                    Cookies.set('schoolType', data.schoolType);
                    Cookies.set('roleId', data.roleId);
                    Cookies.set('oldRoleId', data.roleId);// 保存old角色ID 登陆时默认old角色ID为roleID by cuihd 20180620
                    Cookies.set('roleType', data.roleType);
                    // Cookies.set('clientType', '1:1'); //为了统计在线用户的信息(后期修改) by cuihd 20171229
                    Cookies.set('teacherName', data.userName); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
                    Cookies.set('cardIsOpen', data.cardIsOpen); //查询学校是否开通服务成功
                   
                    // 多邦对接登陆保存数据
                    Cookies.set('sIdentityId', data.sIdentityId);
                    Cookies.set('authDuobang', data.authDuobang);
                   
                    commit('SET_TEACHERNAME', data.userName); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
                    // commit('SET_CLIENTTYPE', '1:1'); //为了统计在线用户的信息(后期修改) by cuihd 20171229
                    commit('SET_CARDISOPEN', data.cardIsOpen);                   
                    commit('SET_ROLEID', data.roleId);
                    commit('SET_OLDROLEID', data.roleId);// 保存old角色ID 登陆时默认old角色ID为roleID by cuihd 20180620
                    commit('SET_UID', data.userId);
                    commit('SET_TOKEN', data.token);
                    commit('SET_USER_TYPE', data.userType);
                    commit('SET_SCHOOLTYPE', data.schoolType);
                    commit('SET_SCHOOLID', data.schoolId);
                    commit('SET_SCHOOLNAME', data.schoolName);
                    commit('SET_ROLETYPE', data.roleType);

                    commit('SET_THIRD_PLATFORM', { 'sIdentityId': data.sIdentityId, authDuobang: data.authDuobang })
                    if (isRember) {
                        Cookies.set('username', username, 30);
                        commit('SET_USERNAME', username, 30);
                    } else {
                        Cookies.remove('username');
                        commit('SET_USERNAME', '');
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // 前台登录
        exueLoginByUsername({ commit }, userInfo) {
            const username = userInfo.exueUsername.trim();
            const password = sha256(userInfo.exuePassword.trim());
            const isRember = userInfo.exueRememberUname;
            return new Promise((resolve, reject) => {
                exueLogin(username, password).then(response => {
                    const data = response.data.content;
                    if (isRember) {
                        Cookies.set('exueUsername', username, 30);
                        commit('SET_EXUEUSERNAME', username, 30);
                    } else {
                        Cookies.remove('exueUsername');
                        commit('SET_EXUEUSERNAME', '');
                    }
                    resolve(data);
                }).catch(error => {
                    reject(error);
                });
            });
        },

        // 第三方登陆
        exueLoginByToken({commit}, token) {
            return new Promise((resolve, reject) => {
                checkThirdLogin(token).then(response => {
                    const data = response.data.content;
                    Cookies.set('uid', data.userId);
                    Cookies.set('accessToken', data.token);
                    Cookies.set('userType', data.userType);
                    Cookies.set('schoolId', data.schoolId);
                    Cookies.set('schoolName', data.schoolName);
                    Cookies.set('schoolType', data.schoolType);
                    Cookies.set('roleId', data.roleId);
                    Cookies.set('oldRoleId', data.roleId);// 保存old角色ID 登陆时默认old角色ID为roleID by cuihd 20180620
                    Cookies.set('roleType', data.roleType);
                    // Cookies.set('clientType', '1:1'); //为了统计在线用户的信息(后期修改) by cuihd 20171229
                    Cookies.set('teacherName', data.userName); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
                    Cookies.set('cardIsOpen', data.cardIsOpen); //查询学校是否开通服务成功
                    commit('SET_TEACHERNAME', data.userName); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
                    // commit('SET_CLIENTTYPE', '1:1'); //为了统计在线用户的信息(后期修改) by cuihd 20171229
                    commit('SET_CARDISOPEN', data.cardIsOpen);
                    commit('SET_ROLEID', data.roleId);
                    commit('SET_OLDROLEID', data.roleId);// 保存old角色ID 登陆时默认old角色ID为roleID by cuihd 20180620
                    commit('SET_UID', data.userId);
                    commit('SET_TOKEN', data.token);
                    commit('SET_USER_TYPE', data.userType);
                    commit('SET_SCHOOLTYPE', data.schoolType);
                    commit('SET_SCHOOLID', data.schoolId);
                    commit('SET_SCHOOLNAME', data.schoolName);
                    commit('SET_ROLETYPE', data.roleType);
                    
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });

        },
        // 获取用户信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                getInfo(state.roleId).then(response => {
                    // const data = response.data.content;
                    commit('SET_MENU_GENERATED', true);
                    // commit('SET_NAME', data.upmsUser.realname);
                    // commit('SET_AVATAR', data.upmsUser.avatar);
                    // commit('SET_UID', data.roleId);
                    // commit('SET_INTRODUCTION', data.introduction);
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // GetInfo({ commit, state }) {
        //   return new Promise((resolve, reject) => {
        //     getInfo(state.token,state.uid).then(response => {
        //       const data = response.data;
        //       commit('SET_ROLES', data.role);
        //       commit('SET_NAME', data.name);
        //       commit('SET_AVATAR', data.avatar);
        //       commit('SET_UID', data.uid);
        //       commit('SET_INTRODUCTION', data.introduction);
        //       resolve(response);
        //     }).catch(error => {      
        //       reject(error);
        //     });
        //   });
        // },

        // 登出
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('SET_UID', '');
                    commit('SET_TOKEN', '');
                    commit('SET_ROLES', []);
                    commit('SET_USER_TYPE', null);
                    // commit('SET_CLIENTTYPE', ''); //为了统计在线用户的信息 by cuihd 20171229
                    commit('SET_TEACHERNAME', ''); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
                    commit('SET_CARDISOPEN', '')
                    Cookies.remove('teacherName'); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
                    Cookies.remove('cardIsOpen');
                    // Cookies.remove('clientType'); //为了统计在线用户的信息 by cuihd 20171229
                    Cookies.remove('uid');
                    Cookies.remove('accessToken');
                    Cookies.remove('userType');
                    Cookies.remove('roleType');
                    if(Cookies.get('frameId')) {
                        Cookies.remove('frameId');
                        Cookies.remove('shareId');
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },

        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('SET_UID', '');
                commit('SET_TOKEN', '');
                // commit('SET_CLIENTTYPE', ''); //为了统计在线用户的信息 by cuihd 20171229
                commit('SET_TEACHERNAME', ''); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
                Cookies.remove('teacherName'); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
                // Cookies.remove('clientType'); //为了统计在线用户的信息 by cuihd 20171229
                Cookies.remove('uid');
                Cookies.remove('accessToken');
                Cookies.remove('userType');
                Cookies.remove('roleType');
                resolve();
            });
        },

        // 动态修改权限
        ChangeRole({ commit }, role) {
            return new Promise(resolve => {
                commit('SET_USER_TYPE', role);
                commit('SET_ROLES', [role]);
                commit('SET_TOKEN', role);
                Cookies.set('accessToken', role);
                Cookies.set('userType', role)
                resolve();
            });
        },

        SetRoleId({ commit }, roleId) {
            Cookies.set('roleId', roleId);
            commit('SET_ROLEID', roleId);
        },
        // SET_THIRD_PLATFORM({commit}, json) {
        //     Cookies.set('authDuobang', json.authDuobang);
        //     Cookies.set('sIdentityId', json.sIdentityId);
        //     commit('SET_THIRD_PLATFORM', json);
        // },
        SetOldRoleId({ commit }, oldRoleId) {  // add by cuihd 20180620
            Cookies.set('oldRoleId', oldRoleId);
            commit('SET_OLDROLEID', oldRoleId);
        },

        SetRoleType({ commit }, roleType) {
            // console.log('set...2222, roleType', roleType)
            Cookies.set('roleType', roleType);
            commit('SET_ROLETYPE', roleType);
        },

        SetSchoolId({ commit }, schoolId) {
            Cookies.set('schoolId', schoolId);
            commit('SET_SCHOOLID', schoolId);
        },

        SetSchoolName({ commit }, schoolName) {
            Cookies.set('schoolName', schoolName);
            commit('SET_SCHOOLNAME', schoolName);
        },

        SetSchoolType({ commit }, schoolType) {
            Cookies.set('schoolType', schoolType);
            commit('SET_SCHOOLTYPE', schoolType);
        },

        SetSchoolUserId({ commit }, userId) {
            Cookies.set('uid', userId);
            commit('SET_UID', userId);
        },
        SetAccessToken({ commit }, token) {
            Cookies.set('accessToken', token);
            commit('SET_TOKEN', token);
        },

        SetSchoolList({ commit }, schoolList) {
            return new Promise(resolve => {
                commit('SET_SCHOOL_LIST', schoolList);
                resolve();
            });
        },

        SetTeacherName({ commit }, teacherName) {
            Cookies.set('teacherName', teacherName); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
            commit('SET_TEACHERNAME', teacherName); // 为了在班主任状态时班级默认显示班主任名 by cuihd 20180115
        },

        SetcardIsOpen({ commit }, cardIsOpen) {
            Cookies.set('cardIsOpen', cardIsOpen);
            commit('SET_CARDISOPEN', cardIsOpen)
        }
    }
};

export default user;